<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <h2>{{message}}</h2>
    <cpn @item-click="cpnClick"></cpn>
</div>

<template id="cpn">
    <div>
        <button v-for="item in categories"
                @click="btnClick(item)">
            {{item.name}}
        </button>
    </div>
</template>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>

    // 1.子组件
    const cpn = {
        template: '#cpn',
        data() {
            return {
                categories: [
                    {id: 'a', name: '热门推荐'},
                    {id: 'b', name: '手机数码'},
                    {id: 'c', name: '家用家电'},
                    {id: 'd', name: '电脑办公'},
                ]
            }
        },
        methods: {
            btnClick(item) {
                // 发射一个事件(事件名，参数)
                this.$emit('item-click', item);
            }
        }
    }

    // 2.父组件
    const app = new Vue({
        el: '#app',
        data: {
            message: 'simplelu'
        },
        methods: {
            cpnClick(item) {
                console.log(item);
            }
        },
        components: {
            cpn
        }
    })
</script>
</body>
</html>